package com.inei.evaluacionemype.model.dao;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.evaluacionemype.model.connection.UtilDB;
import com.inei.evaluacionemype.model.entity.Cap02_03;

public class Cap02_03Dao implements ICRUD<Cap02_03> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public Cap02_03Dao(Context context) {
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public Cap02_03 buscar(Cap02_03 o) {
		Cap02_03 obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery("select * from Cap02_03 where RUC=?",
				new String[] { o.getRUC() + "" });
		if (cursor.moveToFirst()) {
			obj = new Cap02_03();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setC2P1_701(cursor.getDouble(cursor.getColumnIndex("C2P1_701")));
			obj.setC2P2_702(cursor.getDouble(cursor.getColumnIndex("C2P2_702")));
			obj.setC2P3_703(cursor.getDouble(cursor.getColumnIndex("C2P3_703")));
			obj.setC2P4_704(cursor.getDouble(cursor.getColumnIndex("C2P4_704")));
			obj.setC2P5_75(cursor.getDouble(cursor.getColumnIndex("C2P5_75")));
			obj.setC2P6_TOTAL(cursor.getDouble(cursor
					.getColumnIndex("C2P6_TOTAL")));
			obj.setC2P7_601(cursor.getDouble(cursor.getColumnIndex("C2P7_601")));
			obj.setC2P8_602(cursor.getDouble(cursor.getColumnIndex("C2P8_602")));
			obj.setC2P9_62(cursor.getDouble(cursor.getColumnIndex("C2P9_62")));
			obj.setC2P10_63(cursor.getDouble(cursor.getColumnIndex("C2P10_63")));
			obj.setC2P11_64(cursor.getDouble(cursor.getColumnIndex("C2P11_64")));
			obj.setC2P12_65(cursor.getDouble(cursor.getColumnIndex("C2P12_65")));
			obj.setC2P13_TOTAL(cursor.getDouble(cursor
					.getColumnIndex("C2P13_TOTAL")));
			obj.setC3P1_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P1_01_TOTAL")));
			obj.setC3P1_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P1_02_HOMBRES")));
			obj.setC3P1_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P1_03_MUJERES")));
			obj.setC3P1_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P1_04_TOTGASTOS")));
			obj.setC3P2_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P2_01_TOTAL")));
			obj.setC3P2_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P2_02_HOMBRES")));
			obj.setC3P2_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P2_03_MUJERES")));
			obj.setC3P2_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P2_04_TOTGASTOS")));
			obj.setC3P2A_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P2A_01_TOTAL")));
			obj.setC3P2A_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P2A_02_HOMBRES")));
			obj.setC3P2A_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P2A_03_MUJERES")));
			obj.setC3P2A_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P2A_04_TOTGASTOS")));
			obj.setC3P2B_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P2B_01_TOTAL")));
			obj.setC3P2B_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P2B_02_HOMBRES")));
			obj.setC3P2B_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P2B_03_MUJERES")));
			obj.setC3P2B_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P2B_04_TOTGASTOS")));
			obj.setC3P3_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P3_01_TOTAL")));
			obj.setC3P3_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P3_02_HOMBRES")));
			obj.setC3P3_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P3_03_MUJERES")));
			obj.setC3P3_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P3_04_TOTGASTOS")));
			obj.setC3P3A_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P3A_01_TOTAL")));
			obj.setC3P3A_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P3A_02_HOMBRES")));
			obj.setC3P3A_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P3A_03_MUJERES")));
			obj.setC3P3A_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P3A_04_TOTGASTOS")));
			obj.setC3P3B_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P3B_01_TOTAL")));
			obj.setC3P3B_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P3B_02_HOMBRES")));
			obj.setC3P3B_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P3B_03_MUJERES")));
			obj.setC3P3B_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3P3B_04_TOTGASTOS")));
			obj.setC3TOTAL_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3TOTAL_01_TOTAL")));
			obj.setC3TOTAL_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3TOTAL_02_HOMBRES")));
			obj.setC3TOTAL_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3TOTAL_03_MUJERES")));
			obj.setC3TOTAL_04_TOTGASTOS(cursor.getDouble(cursor
					.getColumnIndex("C3TOTAL_04_TOTGASTOS")));
			obj.setC3P4_01_TOTAL(cursor.getInt(cursor
					.getColumnIndex("C3P4_01_TOTAL")));
			obj.setC3P4_02_HOMBRES(cursor.getInt(cursor
					.getColumnIndex("C3P4_02_HOMBRES")));
			obj.setC3P4_03_MUJERES(cursor.getInt(cursor
					.getColumnIndex("C3P4_03_MUJERES")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Cap02_03> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Cap02_03 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("RUC", o.getRUC());
		valores.put("C2P1_701", o.getC2P1_701());
		valores.put("C2P2_702", o.getC2P2_702());
		valores.put("C2P3_703", o.getC2P3_703());
		valores.put("C2P4_704", o.getC2P4_704());
		valores.put("C2P5_75 ", o.getC2P5_75());
		valores.put("C2P6_TOTAL", o.getC2P6_TOTAL());
		valores.put("C2P7_601", o.getC2P7_601());
		valores.put("C2P8_602", o.getC2P8_602());
		valores.put("C2P9_62 ", o.getC2P9_62());
		valores.put("C2P10_63", o.getC2P10_63());
		valores.put("C2P11_64", o.getC2P11_64());
		valores.put("C2P12_65", o.getC2P12_65());
		valores.put("C2P13_TOTAL", o.getC2P13_TOTAL());
		valores.put("C3P1_01_TOTAL", o.getC3P1_01_TOTAL());
		valores.put("C3P1_02_HOMBRES", o.getC3P1_02_HOMBRES());
		valores.put("C3P1_03_MUJERES", o.getC3P1_03_MUJERES());
		valores.put("C3P1_04_TOTGASTOS", o.getC3P1_04_TOTGASTOS());
		valores.put("C3P2_01_TOTAL", o.getC3P2_01_TOTAL());
		valores.put("C3P2_02_HOMBRES", o.getC3P2_02_HOMBRES());
		valores.put("C3P2_03_MUJERES", o.getC3P2_03_MUJERES());
		valores.put("C3P2_04_TOTGASTOS", o.getC3P2_04_TOTGASTOS());
		valores.put("C3P2A_01_TOTAL", o.getC3P2A_01_TOTAL());
		valores.put("C3P2A_02_HOMBRES", o.getC3P2A_02_HOMBRES());
		valores.put("C3P2A_03_MUJERES", o.getC3P2A_03_MUJERES());
		valores.put("C3P2A_04_TOTGASTOS", o.getC3P2A_04_TOTGASTOS());
		valores.put("C3P2B_01_TOTAL", o.getC3P2B_01_TOTAL());
		valores.put("C3P2B_02_HOMBRES", o.getC3P2B_02_HOMBRES());
		valores.put("C3P2B_03_MUJERES", o.getC3P2B_03_MUJERES());
		valores.put("C3P2B_04_TOTGASTOS", o.getC3P2B_04_TOTGASTOS());
		valores.put("C3P3_01_TOTAL", o.getC3P3_01_TOTAL());
		valores.put("C3P3_02_HOMBRES", o.getC3P3_02_HOMBRES());
		valores.put("C3P3_03_MUJERES", o.getC3P3_03_MUJERES());
		valores.put("C3P3_04_TOTGASTOS", o.getC3P3_04_TOTGASTOS());
		valores.put("C3P3A_01_TOTAL", o.getC3P3A_01_TOTAL());
		valores.put("C3P3A_02_HOMBRES", o.getC3P3A_02_HOMBRES());
		valores.put("C3P3A_03_MUJERES", o.getC3P3A_03_MUJERES());
		valores.put("C3P3A_04_TOTGASTOS", o.getC3P3A_04_TOTGASTOS());
		valores.put("C3P3B_01_TOTAL", o.getC3P3B_01_TOTAL());
		valores.put("C3P3B_02_HOMBRES", o.getC3P3B_02_HOMBRES());
		valores.put("C3P3B_03_MUJERES", o.getC3P3B_03_MUJERES());
		valores.put("C3P3B_04_TOTGASTOS", o.getC3P3B_04_TOTGASTOS());
		valores.put("C3TOTAL_01_TOTAL", o.getC3TOTAL_01_TOTAL());
		valores.put("C3TOTAL_02_HOMBRES", o.getC3TOTAL_02_HOMBRES());
		valores.put("C3TOTAL_03_MUJERES", o.getC3TOTAL_03_MUJERES());
		valores.put("C3TOTAL_04_TOTGASTOS", o.getC3TOTAL_04_TOTGASTOS());
		valores.put("C3P4_01_TOTAL", o.getC3P4_01_TOTAL());
		valores.put("C3P4_02_HOMBRES", o.getC3P4_02_HOMBRES());
		valores.put("C3P4_03_MUJERES", o.getC3P4_03_MUJERES());

		resultado = (int) db.insert("Cap02_03", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Cap02_03 o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Cap02_03 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C2P1_701", o.getC2P1_701());
		valores.put("C2P2_702", o.getC2P2_702());
		valores.put("C2P3_703", o.getC2P3_703());
		valores.put("C2P4_704", o.getC2P4_704());
		valores.put("C2P5_75 ", o.getC2P5_75());
		valores.put("C2P6_TOTAL", o.getC2P6_TOTAL());
		valores.put("C2P7_601", o.getC2P7_601());
		valores.put("C2P8_602", o.getC2P8_602());
		valores.put("C2P9_62 ", o.getC2P9_62());
		valores.put("C2P10_63", o.getC2P10_63());
		valores.put("C2P11_64", o.getC2P11_64());
		valores.put("C2P12_65", o.getC2P12_65());
		valores.put("C2P13_TOTAL", o.getC2P13_TOTAL());
		valores.put("C3P1_01_TOTAL", o.getC3P1_01_TOTAL());
		valores.put("C3P1_02_HOMBRES", o.getC3P1_02_HOMBRES());
		valores.put("C3P1_03_MUJERES", o.getC3P1_03_MUJERES());
		valores.put("C3P1_04_TOTGASTOS", o.getC3P1_04_TOTGASTOS());
		valores.put("C3P2_01_TOTAL", o.getC3P2_01_TOTAL());
		valores.put("C3P2_02_HOMBRES", o.getC3P2_02_HOMBRES());
		valores.put("C3P2_03_MUJERES", o.getC3P2_03_MUJERES());
		valores.put("C3P2_04_TOTGASTOS", o.getC3P2_04_TOTGASTOS());
		valores.put("C3P2A_01_TOTAL", o.getC3P2A_01_TOTAL());
		valores.put("C3P2A_02_HOMBRES", o.getC3P2A_02_HOMBRES());
		valores.put("C3P2A_03_MUJERES", o.getC3P2A_03_MUJERES());
		valores.put("C3P2A_04_TOTGASTOS", o.getC3P2A_04_TOTGASTOS());
		valores.put("C3P2B_01_TOTAL", o.getC3P2B_01_TOTAL());
		valores.put("C3P2B_02_HOMBRES", o.getC3P2B_02_HOMBRES());
		valores.put("C3P2B_03_MUJERES", o.getC3P2B_03_MUJERES());
		valores.put("C3P2B_04_TOTGASTOS", o.getC3P2B_04_TOTGASTOS());
		valores.put("C3P3_01_TOTAL", o.getC3P3_01_TOTAL());
		valores.put("C3P3_02_HOMBRES", o.getC3P3_02_HOMBRES());
		valores.put("C3P3_03_MUJERES", o.getC3P3_03_MUJERES());
		valores.put("C3P3_04_TOTGASTOS", o.getC3P3_04_TOTGASTOS());
		valores.put("C3P3A_01_TOTAL", o.getC3P3A_01_TOTAL());
		valores.put("C3P3A_02_HOMBRES", o.getC3P3A_02_HOMBRES());
		valores.put("C3P3A_03_MUJERES", o.getC3P3A_03_MUJERES());
		valores.put("C3P3A_04_TOTGASTOS", o.getC3P3A_04_TOTGASTOS());
		valores.put("C3P3B_01_TOTAL", o.getC3P3B_01_TOTAL());
		valores.put("C3P3B_02_HOMBRES", o.getC3P3B_02_HOMBRES());
		valores.put("C3P3B_03_MUJERES", o.getC3P3B_03_MUJERES());
		valores.put("C3P3B_04_TOTGASTOS", o.getC3P3B_04_TOTGASTOS());
		valores.put("C3TOTAL_01_TOTAL", o.getC3TOTAL_01_TOTAL());
		valores.put("C3TOTAL_02_HOMBRES", o.getC3TOTAL_02_HOMBRES());
		valores.put("C3TOTAL_03_MUJERES", o.getC3TOTAL_03_MUJERES());
		valores.put("C3TOTAL_04_TOTGASTOS", o.getC3TOTAL_04_TOTGASTOS());
		valores.put("C3P4_01_TOTAL", o.getC3P4_01_TOTAL());
		valores.put("C3P4_02_HOMBRES", o.getC3P4_02_HOMBRES());
		valores.put("C3P4_03_MUJERES", o.getC3P4_03_MUJERES());

		resultado = (int) db.update("Cap02_03", valores, "RUC=?",
				new String[] { o.getRUC() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
